var express = require('express');
var db = require('./db.js')

var router = express.Router()
var bodyParser = require('body-parser');
var md5 = require('md5-nodejs');
var urlencodedParser = bodyParser.urlencoded({ extended: false })

var multer = require('multer');

//获取列表
router.get('/randomList', async function (req, res) {
  var sql = `select * from questions order by rand() limit 50`
  let data = await db.doSql(sql)
  res.send(JSON.stringify(db.hand(data)))
})

// 提交成绩
router.post('/score', urlencodedParser, async function (req, res) {
  var { uid, score, total_time, paper } = req.body
  var sql = `insert into score (uid, score, total_time, paper) values ('${uid}','${score}','${total_time}','${paper}')`
  let data = await db.doSql(sql)
  res.send(JSON.stringify(db.hand(data)))
})

module.exports = router